package com.zhu.shop.app.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import com.zhu.shop.app.feign.ShopGoodsFeign;
import com.zhu.shop.app.feign.ShopOrderFeign;
import com.zhu.shop.app.feign.ShopUserFeign;
import com.zhu.shop.app.model.params.BuyParams;
import com.zhu.shop.common.Vo.Result;
import com.zhu.shop.common.Vo.goods.GoodsVo;
import com.zhu.shop.common.Vo.order.OrderParams;
import com.zhu.shop.common.Vo.user.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class BuyServiceImpl  implements  BuyService{
    //nacos也集成了restTemplate
    @Autowired
    private RestTemplate restTemplate; //快于http调用微服务
    @Autowired
    private ShopUserFeign shopUserFeign;
    @Autowired
    private ShopGoodsFeign shopGoodsFeign;
    @Autowired
    private ShopOrderFeign shopOrderFeign;
    private String shopUser="shop-user";
    private String shopGoods="shop-goods";
    private String shopOrder="shop-order";
    /**
     * 下订单
     * @param buyParams
     * @return
     */
    @Override
    public Result submitOrder(BuyParams buyParams) {
        /**
         *1 根据用户id查询用户是否存在
         */
        //
        Result<UserVo> userVoResult = shopUserFeign.findUser(buyParams.getUserId());
        if(null==userVoResult){
            return Result.fail(10001,"用户不存在");
        }
        UserVo userVo = userVoResult.getData();

        /**
         * 2根据商品id查询商品是否存在
         */
        Result<GoodsVo> goodsVoResult = shopGoodsFeign.findGoodsById(buyParams.getGoodsId());
        if(null==goodsVoResult){
            return Result.fail(10001,"商品不存在");
        }
        GoodsVo goodsVo = goodsVoResult.getData();
        /**
         * 3判断商品的库存
         *
         */
        if(goodsVo.getGoodsStock()<=0){
            return Result.fail(10003,"商品库存不足！");
        }
        /**
         * 4判断用户的账户余额
         */
        if(userVo.getAccount().compareTo(goodsVo.getGoodsPrice())==-1){
            return Result.fail(10004,"余额不足！");
        }
        /**
         *5调用订单微服务创建订单
         */
        OrderParams orderParams=new OrderParams();
        orderParams.setUserId(userVo.getId());
        orderParams.setGoodsId(goodsVo.getId());
        orderParams.setGoodsPrice(goodsVo.getGoodsPrice());
        Result<String> stringResult = shopOrderFeign.createOrder(orderParams);
        if(null==stringResult) {
            return Result.fail(10001, "订单创建失败");
        }

        String orderId = stringResult.getData();
        return Result.success(orderId);
    }
//    @Override
//    public Result submitOrder(BuyParams buyParams) {
//        /**
//         *1 根据用户id查询用户是否存在
//         */
//        //
//        String userJson = restTemplate.getForObject("http://localhost:8001/user/findUser/" + buyParams.getUserId(), String.class);
//        Result<UserVo> userVoResult = JSON.parseObject(userJson, new TypeReference<Result<UserVo>>(){});
//        if(null==userVoResult){
//            return Result.fail(10001,"用户不存在");
//        }
//        UserVo userVo = userVoResult.getData();
//
//        /**
//         * 2根据商品id查询商品是否存在
//         */
//        String goodsJson = restTemplate.getForObject("http://localhost:8002/goods/findGoods/" + buyParams.getGoodsId(), String.class);
//        Result<GoodsVo> goodsVoResult = JSON.parseObject(goodsJson, new TypeReference<Result<GoodsVo>>() {
//        });
//        if(null==goodsVoResult){
//            return Result.fail(10001,"商品不存在");
//        }
//        GoodsVo goodsVo = goodsVoResult.getData();
//        /**
//         * 3判断商品的库存
//         *
//         */
//        if(goodsVo.getGoodsStock()<=0){
//            return Result.fail(10003,"商品库存不足！");
//        }
//        /**
//         * 4判断用户的账户余额
//         */
//        if(userVo.getAccount().compareTo(goodsVo.getGoodsPrice())==-1){
//            return Result.fail(10004,"余额不足！");
//        }
//        /**
//         *5调用订单微服务创建订单
//         */
//        OrderParams orderParams=new OrderParams();
//        orderParams.setUserId(userVo.getId());
//        orderParams.setGoodsId(goodsVo.getId());
//        orderParams.setGoodsPrice(goodsVo.getGoodsPrice());
//        String orderResult=restTemplate.postForObject("http://localhost:8003/order/createOrder/",orderParams,String.class);
//        Result<String> stringResult = JSON.parseObject(orderResult, new TypeReference<Result<String>>() {
//        });
//        if(null==stringResult) {
//            return Result.fail(10001, "订单创建失败");
//        }
//        String orderId = stringResult.getData();
//        return Result.success(orderId);
//    }

//    @Override
//    public Result submitOrder(BuyParams buyParams) {
//        /**
//         *1 根据用户id查询用户是否存在
//         */
//        //
//        String userJson = restTemplate.getForObject("http://"+shopUser+"/user/findUser/" + buyParams.getUserId(), String.class);
//        Result<UserVo> userVoResult = JSON.parseObject(userJson, new TypeReference<Result<UserVo>>(){});
//        if(null==userVoResult){
//            return Result.fail(10001,"用户不存在");
//        }
//        UserVo userVo = userVoResult.getData();
//
//        /**
//         * 2根据商品id查询商品是否存在
//         */
//        String goodsJson = restTemplate.getForObject("http://"+shopGoods+"/goods/findGoods/" + buyParams.getGoodsId(), String.class);
//        Result<GoodsVo> goodsVoResult = JSON.parseObject(goodsJson, new TypeReference<Result<GoodsVo>>() {
//        });
//        if(null==goodsVoResult){
//            return Result.fail(10001,"商品不存在");
//        }
//        GoodsVo goodsVo = goodsVoResult.getData();
//        /**
//         * 3判断商品的库存
//         *
//         */
//        if(goodsVo.getGoodsStock()<=0){
//            return Result.fail(10003,"商品库存不足！");
//        }
//        /**
//         * 4判断用户的账户余额
//         */
//        if(userVo.getAccount().compareTo(goodsVo.getGoodsPrice())==-1){
//            return Result.fail(10004,"余额不足！");
//        }
//        /**
//         *5调用订单微服务创建订单
//         */
//        OrderParams orderParams=new OrderParams();
//        orderParams.setUserId(userVo.getId());
//        orderParams.setGoodsId(goodsVo.getId());
//        orderParams.setGoodsPrice(goodsVo.getGoodsPrice());
//        String orderResult=restTemplate.postForObject("http://"+shopOrder+"/order/createOrder/",orderParams,String.class);
//        Result<String> stringResult = JSON.parseObject(orderResult, new TypeReference<Result<String>>() {
//        });
//        if(null==stringResult) {
//            return Result.fail(10001, "订单创建失败");
//        }
//        String orderId = stringResult.getData();
//        return Result.success(orderId);
//    }

}
